package io.gitee.zhangbinhub.admin.controller.inner

import io.gitee.zhangbinhub.acp.boot.exceptions.WebException
import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import io.gitee.zhangbinhub.admin.api.CommonPath
import io.gitee.zhangbinhub.admin.api.ServerApi
import io.gitee.zhangbinhub.admin.base.BaseController
import io.gitee.zhangbinhub.admin.component.TokenTools
import io.gitee.zhangbinhub.admin.entity.ModuleFunc
import io.gitee.zhangbinhub.admin.service.ModuleFuncService
import io.gitee.zhangbinhub.admin.vo.BooleanInfoVo
import io.gitee.zhangbinhub.admin.vo.TokenUserInfoVo
import io.swagger.v3.oas.annotations.*
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * @author zhang by 16/01/2019
 * @since JDK 11
 */
@Validated
@RestController
@RequestMapping(CommonPath.innerBasePath)
@Tag(name = "权限信息（内部接口）")
class InnerTokenController @Autowired
constructor(
    logAdapter: LogAdapter,
    private val tokenTools: TokenTools,
    private val moduleFuncService: ModuleFuncService
) : BaseController(logAdapter) {
    @Operation(summary = "获取当前用户token信息", description = "根据当前登录的用户token值，返回详细信息")
    @GetMapping(value = [ServerApi.currToken], produces = [MediaType.APPLICATION_JSON_VALUE])
    fun currToken(@Parameter(hidden = true) bearerTokenAuthentication: BearerTokenAuthentication): ResponseEntity<TokenUserInfoVo> =
        ResponseEntity.ok(tokenTools.getUserInfoFromToken(bearerTokenAuthentication))

    @Operation(summary = "获取当前用户所有功能权限信息", description = "根据当前登录的用户信息，查询具备的功能权限")
    @GetMapping(value = [ServerApi.currModuleFunc], produces = [MediaType.APPLICATION_JSON_VALUE])
    @Throws(WebException::class)
    fun currModuleFuncList(@Parameter(hidden = true) bearerTokenAuthentication: BearerTokenAuthentication): ResponseEntity<List<ModuleFunc>> =
        ResponseEntity.ok(
            moduleFuncService.getModuleFuncList(
                tokenTools.getAuthenticatedPrincipal(bearerTokenAuthentication).clientId ?: "",
                bearerTokenAuthentication.name
            )
        )

    @Operation(summary = "判断当前用户是否具有指定的权限")
    @GetMapping(value = [ServerApi.moduleFunc + "/{moduleFuncCode}"], produces = [MediaType.APPLICATION_JSON_VALUE])
    @Throws(WebException::class)
    fun currUserHasModuleFunc(
        @Parameter(hidden = true) bearerTokenAuthentication: BearerTokenAuthentication,
        @PathVariable(name = "moduleFuncCode") moduleFuncCode: String
    ): ResponseEntity<BooleanInfoVo> =
        ResponseEntity.ok(
            BooleanInfoVo(
                result = moduleFuncService.hasModuleFunc(
                    tokenTools.getAuthenticatedPrincipal(bearerTokenAuthentication).clientId ?: "",
                    bearerTokenAuthentication.name,
                    moduleFuncCode
                )
            )
        )
}
